package core

import (
	"database/sql"

	_ "github.com/mattn/go-sqlite3"
)

// LoadChannels 加载所有通道的配置信息
func LoadChannels() []Channel {
	db, err := sql.Open("sqlite3", G.App.DBPath)
	if err != nil {
		G.Log.Errorf("Open Database [%s] Failed: %v", G.App.DBPath, err)
		panic(err)
	}

	defer db.Close()

	//查询通道数据
	rows, err := db.Query("SELECT * FROM channels")
	if err != nil {
		G.Log.Errorf("Query Channels Failed: %v", err)
		panic(err)
	}

	var channels = make([]Channel, 0)

	for rows.Next() {
		var c Channel
		_ = rows.Scan(&c.Id, &c.Code, &c.Status, &c.IsControllable)
		channels = append(channels, c)
	}

	return channels
}
